<?php

/**
 * This is the model class for table "admin".
 *
 * The followings are the available columns in table 'admin':
 * @property string $id
 * @property string $role_id
 * @property string $user_name
 * @property string $password
 * @property string $salt
 * @property string $user_email
 * @property string $display_name
 * @property string $login_time
 * @property string $user_registered
 */
class Admin extends CActiveRecord {

    public $password_repeat;
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Admin the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{admin}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_name, salt, role_id', 'required'),
            array('user_name', 'unique'),
            array('user_name, display_name', 'length', 'min'=>3, 'max' => 50),
            array('password', 'compare', 'compareAttribute' => 'user_name', 'operator' => '!=', 'message' => '密码不能和用户名相同'),
            array('password, password_repeat', 'required', 'on'=>'insert'),
            array('password', 'compare', 'compareAttribute' => 'password_repeat', 'message' => '两次密码不一致！'),
            array('salt', 'length', 'max' => 16),
            array('user_email', 'email'),
            array('login_time, user_registered, password, password_repeat', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, role_id, user_name, salt, user_email, display_name, login_time, user_registered', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'role_id' => '分组',
            'user_name' => '用户名',
            'password' => '密码',
            'password_repeat' => '确认密码',
            'salt' => '加密串',
            'user_email' => '邮箱',
            'display_name' => '姓名',
            'login_time' => '最后登录时间',
            'user_registered' => '注册时间',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('role_id', $this->role_id);
        $criteria->compare('user_name', $this->user_name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('salt', $this->salt, true);
        $criteria->compare('user_email', $this->user_email, true);
        $criteria->compare('display_name', $this->display_name, true);
        $criteria->compare('login_time', $this->login_time, true);
        $criteria->compare('user_registered', $this->user_registered, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    /**
     * 
     */

    /**
     * 生成密码
     * @param type $pass
     * @return type 
     */
    public function generationPassword($pass, $salt) {
        return md5($pass . $salt);
    }

}